/* James McTaggart (jcm28) Informatics
Multimedia Communications Technology - 17th May 2007
In making this submission I declare that my work contains no examples
of misconduct, such as plagiarism, collusion, or fabrication of results.
*/

package client;

import java.net.DatagramPacket;
import java.net.InetAddress;

public final class ClientProtocol {

	public static DatagramPacket get101(InetAddress serverIP, int serverPort){
		byte[] outData = new byte[60];
		String header = "101CONNECT";
		System.arraycopy(header.getBytes(), 0, outData, 0, header.length());
		return new DatagramPacket(outData,outData.length,serverIP,serverPort);
	}
	
	public static DatagramPacket get102(InetAddress serverIP, int serverPort){
		byte[] outData = new byte[60];
		String header = "102DISCONNECT";
		System.arraycopy(header.getBytes(), 0, outData, 0, header.length());
		return new DatagramPacket(outData,outData.length,serverIP,serverPort);
	}
	
	public static DatagramPacket get301(InetAddress serverIP, int serverPort) {
		byte[] outData = new byte[60];
		//Create the header (for this packet the data section is empty)
		System.arraycopy("301START".getBytes(), 0, outData, 0, 8);
		return new DatagramPacket(outData, outData.length, serverIP, serverPort);
	}

	public static DatagramPacket get302(int missingSampleNo,InetAddress serverIP, int serverPort) {
		byte[] outData = new byte[60];
		//Create the header for the packet
		System.arraycopy("302RETRANS".getBytes(),0,outData,0,11);
		//The first 6 bytes of the data section contain the number
		System.arraycopy(padNumber(missingSampleNo).getBytes(), 0, outData, 13, 6);
		return new DatagramPacket(outData, outData.length, serverIP, serverPort);
	}

	public static String padNumber(int number){
		String result = "";
		if(number==0)return "000000";
		//Get the length of the number
		int i = (int)Math.floor(Math.log10(number));
		for(int j = 0; j<(5-i); j++)result+="0";
		result += number;
		return result;
	}
}
